package club.janna.jrpc.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Panbo.Guo
 */
public class IDGenerator {
    private static final AtomicInteger COUNTER = new AtomicInteger(0);

    /**
     * 获取id
     * 取值范围 0 ~ Integer.MAX_VALUE
     * @return
     */
    public static int get() {
        int id;
        do {
            id = COUNTER.incrementAndGet();
            if(id > 0)
                return id;
        } while (!COUNTER.compareAndSet(id, 0));
        return 0;
    }
}
